Ext.namespace("Info.Report");

Info.Report.panel = function(infoType) {
	
	this.txtInfoTitle = new Ext.form.TextField({
		width: 100
	});
	this.dtStart = new Ext.form.DateField({
		width: 110,
		format: 'Y-m-d H:i'
	});
	this.dtEnd = new Ext.form.DateField({
		width: 110,
		format: 'Y-m-d H:i'
	});
	this.cmStart = new Ext.form.DateField({
		width: 110,
		format: 'Y-m-d H:i'
	});
	this.cmEnd = new Ext.form.DateField({
		width: 110,
		format: 'Y-m-d H:i'
	});
    
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找信息'},
        handler: this.queryInfo,
        scope: this
    });
    
    this.AllSend = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '全部上报',
        tooltip: {text: '列表信息全部上报'},
        handler: this.sendAllInfo,
        scope: this
    });
    this.btnSend = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '上报',
        tooltip: {text: '信息上报'},
        handler: this.sendInfo,
        scope: this
    });
	 this.btnaddInfo = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '新建信息',
        tooltip: {text: '新建信息'},
        handler: this.showAddInfo,
        scope: this
    });
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../info/draftSearch.form?timesnap=' + Date.parse(new Date())
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'infoId',
			fields: ['infoId', 'infoHeader', 'lastFinishTime','department','infoType', 'infoContent','status',
					'instancyLevel','answerInfo','remark','createUserCode','createUser','createTime',
					'reportGroup','reportTime','instancyLevel','backCause']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	             infoTitle:this.txtInfoTitle.getValue(),
			  		 dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d H:i'):'',
			  		 dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d H:i'):'',
			  		 cmStart:this.cmStart.getValue()?this.cmStart.getValue().format('Y-m-d H:i'):'',
			  		 cmEnd:this.cmEnd.getValue()?this.cmEnd.getValue().format('Y-m-d H:i'):''
       	          });    
              },
              scope:this 
	    	}
		}
	});
	this.renderTime = function(value){
		if(!value)
			return "";
		return new Date(value).format('Y-m-d H:i');
	};
	this.renderDays = function(value){
		if(!value)
			return "";
		var date = Date.parseDate(value,'Y-m-d H:i');
		var curDate = Date.parse(new Date());
		var days = parseInt((date-curDate)/(1000*60*60*24));
		var hours = parseInt( ((date-curDate)%(1000*60*60*24))/(1000*60*60) );
		var minutes = parseInt( ((date-curDate)%(1000*60*60))/(1000*60) );
		if(days<0){
			return "<font color='red'>逾期"+(-days)+"天"+(-hours)+"小时"+(-minutes)+"分钟"+"</font>";
		}
		else if(days==0){
		    if(hours<0){
		        return "<font color='red'>逾期"+(-hours)+"小时"+(-minutes)+"分钟"+"</font>";
		    }
		    else if(hours==0){
		         if(minutes<0){
		              return "<font color='red'>逾期"+(-minutes)+"分钟"+"</font>";
		         }
		         else{
		              return "<font size=1>还剩"+(minutes)+"分钟"+"</font>";
		         }
		    }
		    else{
		         return "<font size=1>还剩"+(hours)+"小时"+(minutes)+"分钟"+"</font>";
		    }
		}
		else 
			return "<font size=1>还剩"+(days)+"天"+(hours)+"小时"+(minutes)+"分钟"+"</font>";
	};
	this.renderInfoType = function(value){
		return OMS.INFO_TYPE[value];
	};
	this.renderInfoInstancyLevel = function(value){
		return OMS.INFO_INSTANCY_LEVEL[value];
	};
	this.renderInfoStatus = function(value){
		return OMS.INFO_STATUS[value];
	};
	this.colModel = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
	   		 new Ext.grid.RowNumberer(),
			{header: 'infoId', width: 50, hidden: true, dataIndex: 'infoId'},
			{header: '信息名称', width: 200, dataIndex: 'infoHeader'},
			{header: '信息类型', dataIndex: 'infoType',renderer:this.renderInfoType},
			{header: '紧急级别', dataIndex: 'instancyLevel',renderer:this.renderInfoInstancyLevel},
			{header: '完成时限', width: 150, dataIndex: 'lastFinishTime'},
			{header: '剩余天数',width: 200,dataIndex: 'lastFinishTime',renderer:this.renderDays},
			{header: '状态', dataIndex: 'status',renderer:this.renderInfoStatus}
	    ]
	});
	
	this.gridPanel = new Ext.grid.GridPanel({
		title: '信息列表',
		region: 'center',
		renderTo:document.body,
		
		cm:this.colModel,
		store:this.store,
		tbar: ['信息名称:', this.txtInfoTitle, 
		          ' 上报时间:', this.dtStart, '~', this.dtEnd, '完成时限:',this.cmStart,'~',this.cmEnd,this.btnSearch,'->',this.btnaddInfo,'-',this.btnSend],
        loadMask: {msg: '加载中，请稍候...'},
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
			scope:this,
			rowdblclick : function( grid, rowIndex,e){ 
				var info =grid.store.getAt(rowIndex).data;
				if(!Info.Add.win.instance)
					Info.Add.win.instance = new Info.Add.win();
				Info.Add.win.instance.initUpdate(info);
				Info.Add.win.instance.show();
			}
		}
	})
	this.sendCon = new Ext.data.Connection({
		url:'../info/sendInfo.form?timesnap=' + Date.parse(new Date()),
		method:'post'
	});
	Info.Report.panel.superclass.constructor.call(this, {
		layout:'border',
		items:[this.gridPanel],
		listeners:{
			render:this.queryInfo
		}
	});
};
Ext.extend(Info.Report.panel, Ext.Viewport, {
	showAddInfo:function(){
		if(!Info.Add.win.instance)
			Info.Add.win.instance = new Info.Add.win();
		Info.Add.win.instance.initAdd();
		Info.Add.win.instance.show();
	},
	showUpdateInfo:function(data){
		if(!Info.Add.win.instance)
			Info.Add.win.instance = new Info.Add.win();
		Info.Add.win.instance.initUpdate(data);
		Info.Add.win.instance.show();
	},
	queryInfo :function(){
		this.store.load({
			params:{
				start : 0,
				limit : 15,
				infoHeader:this.txtInfoTitle.getValue(),
			    dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d H:i'):'',
			    dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d H:i'):'',
			    cmStart:this.cmStart.getValue()?this.cmStart.getValue().format('Y-m-d H:i'):'',
			    cmEnd:this.cmEnd.getValue()?this.cmEnd.getValue().format('Y-m-d H:i'):''
				}
		})
	},
	sendInfo:function(){
				var sm =this.gridPanel.getSelectionModel();
				var record = sm.getSelections();
				if(record.length==0){
					Utils.warn('请选择信息！','警告');
					return ;
				}
				var infoIds = [];
				for(var i =0;i<record.length;i++){
				    var d=record[i].data;
					if(d.lastFinishTime==''||d.infoContent==''||d.reportGroup==''){
					   Utils.warn(d.infoHeader+'的信息内容不全，请填写完整后，再提交!','警告');
					   return;
					 }
					infoIds.push(d.infoId);
				}
				if(!confirm("确定要上报吗？")){
					return;
				}
				Utils.request(this.sendCon,{
					params:{
						infoIds:infoIds
					},
					scope:this,
					success:function(){
						Utils.info('上报成功！');
						this.scope.store.reload();
					}
				})
	},
	sendAllInfo:function(){
		Ext.Msg.confirm("信息","确认全部上报?",function(btn){
			if(btn=="yes"){
				Utils.request(this.deleteInfoConn,{
					params:
					{
						infoId:record[0].data.ID
					},
					scope:this,
					success:function(){
						Utils.info('提示信息','操作成功!');
						this.scope.gridPanel.store.reload();
					},
					failure:function(){
						Utils.error('提示信息','操作失败!');
					}
				});
			}
		},this);
	}
});

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif';
   	Info.Report.panel.instance = new Info.Report.panel();
   
});

